package com.yueyang.sojourn.server.controller.share;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.common.baymax.web.result.exception.AbstractResultException;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.controller.model.JsonResult;
import com.yueyang.sojourn.server.model.enums.ShareDistributorStatusEnum;
import com.yueyang.sojourn.server.model.vo.DistributorImportResultVo;
import com.yueyang.sojourn.server.model.vo.ShareDistributorVo;
import com.yueyang.sojourn.server.service.share.ShareDistributorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "分销员接口")
@RestController
@RequestMapping("/share/distributor")
public class ShareDistributorController extends BaseController {
    @Autowired
    private ShareDistributorService shareDistributorService;

    @ApiOperation(value="分销员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-Token", paramType = "header", required = true),
            @ApiImplicitParam(name = "status",value = "0:未启用 1:已启用 -1:不限", dataType = "string", paramType = "query", example = "0"),
            @ApiImplicitParam(name = "keyword",value = "关键词", dataType = "string", paramType = "query", example = "13388888888"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量", dataType = "int", paramType = "query", example = "30"),
            @ApiImplicitParam(name = "pageNum",value = "页码", dataType = "int", paramType = "query", example = "1")
    })
    @GetMapping("/list")
    public PageInfo<ShareDistributorVo> findList(@RequestParam(value = "status", required = false) Integer status,
                                                 @RequestParam(value = "keyword", required = false) String keyword,
                                                 @RequestParam("pageSize") int pageSize,
                                                 @RequestParam("pageNum") int pageNum) {
        ShareDistributorStatusEnum statusEnum = ShareDistributorStatusEnum.findByValue(status).orElse(null);
        return this.shareDistributorService.findList(statusEnum, keyword, pageSize, pageNum);
    }

    @ApiOperation(value="修改分销员启用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-Token", paramType = "header", required = true),
            @ApiImplicitParam(name = "status",value = "0:未启用 1:已启用", dataType = "string", paramType = "query", example = "0"),
            @ApiImplicitParam(name = "userId",value = "分销员userId", dataType = "long", paramType = "query", example = "123456"),
    })
    @PostMapping("/status")
    public void changeStatus(@RequestParam("userId") Long userId,
                             @RequestParam("status") Integer status) {
        ShareDistributorStatusEnum statusEnum = ShareDistributorStatusEnum.findByValue(status).orElse(null);
        JAssert.pass(statusEnum != null, "status取值" + status + "不存在");
        this.shareDistributorService.updateStatus(userId, statusEnum, getLoginUserId());
    }

    @ApiOperation(value="分销员导入")
    @PostMapping("/dataImport")
    public JsonResult<DistributorImportResultVo> dataImport(@RequestParam("fileName") String fileName) {
        try {
            return ok().of(this.shareDistributorService.dataImport(fileName, getLoginUserId()));
        } catch (AbstractResultException e) {
            return fail(e.getResult().getMessage());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @ApiOperation(value="获取分销员导入模板文件地址")
    @GetMapping("/getTemplate")
    public String getTemplate() {
        return "http://cdn.yueyangshuyuan.com/%E5%88%86%E9%94%80%E5%91%98%E5%AF%BC%E5%85%A5-%E6%A8%A1%E6%9D%BF.xlsx";
    }
}
